
package com.ukao.pad.presenter;

import android.content.Context;
import android.widget.RadioButton;

import com.ukao.pad.base.BasePresenter;
import com.ukao.pad.bean.ClothesOperateStatusBean;
import com.ukao.pad.bean.ReceiptBean;
import com.ukao.pad.bean.RecheckBean;
import com.ukao.pad.bean.StrikeBean;
import com.ukao.pad.bean.StringBean;
import com.ukao.pad.consts.Constant;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.retrofit.ApiCallback;
import com.ukao.pad.utils.Utils;
import com.ukao.pad.view.PackPackagView;

import java.util.HashMap;

import okhttp3.ResponseBody;

import static com.ukao.pad.R.id.model;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class PackPackagPesenter extends BasePresenter<PackPackagView> {


    public PackPackagPesenter(PackPackagView view) {
        attachView(view);
    }



    /*工厂复检返工*/
    public void stockRework(String scanCode  ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("scanCode",scanCode);/*衣物条码*/
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.stockRework(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.loadReworkSucceed(model.getMsg());

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }


    /*搜索条码*/
    public void stationScanCode(Context context, String  batchCode  ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("scanCode",batchCode);/*衣物条码*/
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.stationScanCodePack(Constant.createParameter(mHashMap)),
                new ApiCallback<RecheckBean>() {
                    @Override
                    public void onSuccess(RecheckBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.loadSearchData(model.getData());
                        }else{
                            mvpView.loadReworkFail(model.getMsg());
                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }
    /**
     * 衣物合格证
     */
    public void conformity(Context context, String  orderProId  ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderProId",orderProId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.conformity(Constant.createParameter(mHashMap)),
                new ApiCallback<StrikeBean>(){


                    @Override
                    public void onSuccess(StrikeBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.loadStrikeSucceed(model.getData());

                        }else{
                            mvpView.loadReworkFail(model.getMsg());

                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }


    /**
     * 设置上架号
     * @param context
     * @param orderId   订单id
     * @param number   上架号
     */
    public void setPutaway(Context context, String  orderId ,final  String number ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("number",number);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.setPutaway(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>(){


                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.loadSetPutaway(number,model.getMsg());

                        }else{
                            mvpView.loadReworkFail(model.getMsg());

                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }

    /**
     * 释放上架号
     * @param context
     * @param orderId   订单id
     *
     */
    public void downShelf(Context context, String  orderId ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.downShelf(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>(){
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.downShelf(model.getMsg());

                        }else{
                            mvpView.loadReworkFail(model.getMsg());

                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }





    /**
     * 设置自动上架还是手动上架

     */
    public void factoryMachineupdate(Context context,final String  shelfMode ,final RadioButton mRadioButton) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("shelfMode",shelfMode);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.factoryMachineupdate(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>(){
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {

                             mvpView.putAwaySucceed(shelfMode);
                        }else{

                            mvpView.putawayFail(model.getMsg(),mRadioButton);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mvpView.putawayFail("",mRadioButton);
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }
    /**
     * 打印小票
     * @param context
     * @param orderId
     */
    public void printReceipt(Context context, String  orderId   ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.printReceipt(Constant.createParameter(mHashMap)),
                new ApiCallback<ReceiptBean>(){

                    @Override
                    public void onSuccess(ReceiptBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.loadReceipt(model.getData());

                        }else{
                            mvpView.loadFail(model.getMsg());

                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }
    /**
     *  请求单个衣物操作记录
     * @param context
     * @param proId
     */
    public void requestSingleClothingOperateRecord(Context context, String  proId,String clothesName,String clothesCode) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("proId",proId);
        mHashMap.put("accessToken", SaveParamets.getToken(context));
        addSubscription(apiStores.singleClothingOperateRecord(Constant.createParameter(mHashMap)),
                new ApiCallback<ClothesOperateStatusBean>(){
                    @Override
                    public void onSuccess(ClothesOperateStatusBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.receiveClothesOperateData(model.getData(),clothesName,clothesCode);
                        }else{
                            mvpView.loadFail(model.getMsg());

                        }
                    }
                    @Override
                    public void onNoNewWork() {
                    }

                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }


}
